#This to obtain:
#Figure_A12a.jpg
#Figure_A13a.jpg
#Figure_A13b.jpg
#Figure_A13c.jpg

rm(list = ls())
library("ggplot2")
library("dplyr")
library("ggrepel")
library("raster")
library("RColorBrewer")
library("lemon")
library("sp")
library("maptools")
library("ggsn")
library("gstat")
library("gtable")
library("raster")
library("gtable")
library("ggrepel")
library("sf")
library("tidyverse")
library("raster")
library("gstat")


#setwd("C:/Users/bogdanp/")
setwd("/Users/bgpopescu/")
#setwd("/Users/bogdan.popescu/")


monuments_data <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="monuments_data2")

massacres_proj<- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                         layer="massacres")

concentration_camps_proj<- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                         layer="concentration_camps")


thor_ww2_croatia_proj<- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                                   layer="thor_ww2_croatia")

HRV_adm0 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                layer="HRV_adm0_wgs")
HRV_adm0<-st_simplify(HRV_adm0,  dTolerance = 0.005)



mil_col_short_polygon <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                  layer="krajna_line_GCS")

occupied <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="occupied_area")
occupied<-st_simplify(occupied,  dTolerance = 0.005)


ucdp_events_croatia <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                               layer="ucdp_events_croatia")

all_coun <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="all_countries_GCS")
sf_use_s2(FALSE)
all_coun<-st_simplify(all_coun,  dTolerance = 0.005)


#Reading Raster
euro_raster <- raster("./Dropbox/Legacies_Project/Analysis/data/euro_raster.tif")

max_lat<-46.669853 + 3
min_lat<-42.4 - 3

max_lon<-19.447201 + 3
min_lon<-13.556495 - 3

e <- extent(min_lon, max_lon, min_lat, max_lat)
rc <- crop(euro_raster, e)

#Lower resolution raster
rc.aggregate <- aggregate(rc, fact=3)
res(rc.aggregate)

#Get rid of really high mountains
rc <- reclassify(rc.aggregate, c(10000,Inf,NA))
rm(rc.aggregate)
rna <- reclassify(rc, cbind(NA, 0))
rm(rc)
#Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(rna); hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Hill")


################################
#MAP 1 Bombing by building Type#
################################

value<-subset(monuments_data, select = c(Figures_in_German_Marks, xcoord, ycoord))
names(value) <- c("value_damage", "x", "y", "Shape")
value_no <- subset(value, !is.na(x) & !is.na(y) & !is.na(value_damage))

#Set spatial coordinates to create a Spatial object:
extent(value_no)

##### IDW interpolation #####
# establish an extent within which you want to interpolate
x_range <- as.numeric(c(extent(HRV_adm0)[1], extent(HRV_adm0)[2]))  # min/max longitude of the interpolation area
y_range <- as.numeric(c(extent(HRV_adm0)[3], extent(HRV_adm0)[4]))  # min/max latitude of the interpolation area

# create an empty grid of values ranging from the xmin-xmax, ymin-ymax
grd <- expand.grid(x = seq(from = x_range[1],
                           to = x_range[2], 
                           by = 0.02),
                   y = seq(from = y_range[1],
                           to = y_range[2], 
                           by = 0.02))  # expand points to grid

coordinates(grd) <- ~x + y
# turn into a spatial pixels object
gridded(grd) <- TRUE
#turn into sf object
grd<-as(grd, "sf")

#### view grid with points overlayed
# plot(grd, cex = 1.5, col = "grey")
# plot(value_no,
#      pch = 15,
#      col = "red",
#      cex = 1, add=T)
# plot(HRV_adm0,add=T)

value_no<-value_no[!sf::st_is_empty(value_no), ]
value_no<-na.omit(value_no)
grd <- sf::st_set_crs(grd, sf::st_crs(value_no))


#IDW interpolation
g = gstat(formula = log(value_damage+1) ~ 1, data = value_no)
z = predict(g, grd)
z2<-subset(z, select = var1.pred)
names(z2)[names(z2)=="var1.pred"]<-"return"

z3 <- z2 %>%
  mutate(lon = unlist(map(z2$geometry,1)),
         lat = unlist(map(z2$geometry,2)))

#This may take a few mins
idw_crp<-st_intersection(z3, HRV_adm0)

yug1<-ggplot()+
  geom_tile(data = idw_crp, 
            alpha = 0.9,
            aes(x = lon,
                y = lat,
                fill = round(return, 0)))+
  scale_fill_gradient(low = "white", high = "black", guide = guide_legend(title = "Log Value of Damage"))+
  geom_point(aes(x = monuments_data$xcoord, y = monuments_data$ycoord, 
                 colour = monuments_data$Damages))+
  geom_sf(data = HRV_adm0, colour = "black", fill=NA, linewidth = 0.8)+
  geom_sf(data = mil_col_short_polygon, fill=NA, colour = "blue", linewidth = 0.9)+
  labs(colour="Building type")+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
           #Above you mention how long the scalebar should be
           y.min = 46.4, y.max = 46.7,
           #Above you mention how thick the scalebar should be
           dist = 50, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)

ggsave(yug1,file="./Dropbox/Legacies_Project/Paper/figures/Figure_A13c.jpg", 
       height=19.42, width=33.2, units = "cm", dpi=300)


#################################
#MAP 2 Material Damage by Amount#
#################################

dam_data<-st_drop_geometry(monuments_data)

yug2<-ggplot()+
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude in m", guide="none")  + 
  geom_sf(data = HRV_adm0, fill=NA, colour = "black", linewidth = 0.8)+
  geom_sf(data = mil_col_short_polygon, fill=NA, colour = "blue", linewidth = 1.5)+
  geom_sf(data = monuments_data, aes(size = Damage_of_degree_1),
          colour = "red", shape=16, alpha=0.6)+
  scale_size(name   = "Damage")+
  scale_fill_manual("Damage", values = "Red", guide = "legend") +
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
           #Above you mention how long the scalebar should be
           y.min = 46.4, y.max = 46.7,
           #Above you mention how thick the scalebar should be
           dist = 50, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)

yug2_x<-reposition_legend(yug2, 'bottom left')

ggsave(yug2_x,file="./Dropbox/Legacies_Project/Paper/figures/Figure_A13b.jpg", 
       height=19.42, width=23.2, units = "cm", dpi=300)


#############
#MAP 3 Bombs#
#############

#Points
massacres_lbl<-subset(massacres_proj, select = c("Name", "Perpetrator", "lon", "lat"))

#Let's duplicate the data table to avoid accidental overwriting. Define latitude and longitude:
value_no <- subset(massacres_proj, !is.na(lon) & !is.na(lat) & !is.na(death_no))
value_no$x <- value_no$lon  # define x & y as longitude and latitude
value_no$y <- value_no$lat


value<-subset(massacres_proj, select = c(death_no , lon, lat))
names(value) <- c("death_no", "x", "y", "Shape")
value_no <- subset(value, !is.na(x) & !is.na(y) & !is.na(death_no))

#Set spatial coordinates to create a Spatial object:
extent(value_no)


##### IDW interpolation #####
# establish an extent within which you want to interpolate
x_range <- as.numeric(c(extent(HRV_adm0)[1], extent(HRV_adm0)[2]))  # min/max longitude of the interpolation area
y_range <- as.numeric(c(extent(HRV_adm0)[3], extent(HRV_adm0)[4]))  # min/max latitude of the interpolation area

# create an empty grid of values ranging from the xmin-xmax, ymin-ymax
grd <- expand.grid(x = seq(from = x_range[1],
                           to = x_range[2], 
                           by = 0.02),
                   y = seq(from = y_range[1],
                           to = y_range[2], 
                           by = 0.02))  # expand points to grid

coordinates(grd) <- ~x + y
# turn into a spatial pixels object
gridded(grd) <- TRUE
#turn into sf object
grd<-as(grd, "sf")

#### view grid with points overlayed
# plot(grd, cex = 1.5, col = "grey")
# plot(value_no,
#      pch = 15,
#      col = "red",
#      cex = 1, add=T)
# plot(HRV_adm0,add=T)
# 

value_no<-value_no[!sf::st_is_empty(value_no), ]
value_no<-na.omit(value_no)
grd <- sf::st_set_crs(grd, sf::st_crs(value_no))


#IDW interpolation
g = gstat(formula = log(death_no+1) ~ 1, data = value_no)
z = predict(g, grd)
z2<-subset(z, select = var1.pred)
names(z2)[names(z2)=="var1.pred"]<-"return"


# plot(z2,
#      col = terrain.colors(55))


z3 <- z2 %>%
  mutate(lon = unlist(map(z2$geometry,1)),
         lat = unlist(map(z2$geometry,2)))

idw_crp<-st_intersection(z3, HRV_adm0)


massacres_proj$Perpetrator<-as.character(massacres_proj$Perpetrator)
massacres_proj$Perpetrator[massacres_proj$Perpetrator == "both Chetniks and Italian forces"]<-"Chetniks & \n Italian forces"
massacres_proj$Perpetrator<-as.factor(massacres_proj$Perpetrator)

ww2_death_no<-ggplot()+
  geom_tile(data = idw_crp, 
            alpha = 0.9,
            aes(x = lon,
                y = lat,
                fill = round(return, 0)))+
  scale_fill_gradient(low = "white", high = "black", guide = guide_legend(title = "Log No. of Deaths"))+
  geom_sf(data = mil_col_short_polygon, colour = "blue", fill = NA, linewidth = 1.5)+
  geom_sf(data = HRV_adm0, colour = "black", fill = NA, linewidth = 0.8)+
  geom_point(aes(x = massacres_proj$lon, y = massacres_proj$lat, 
                 colour = massacres_proj$Perpetrator, size = 3))+
  guides(size="none") +
  geom_text_repel(data = massacres_proj, aes(lon, lat, label = name_massacre))+
  labs(colour="Perpetrator")+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
           #Above you mention how long the scalebar should be
           y.min = 46.4, y.max = 46.7,
           #Above you mention how thick the scalebar should be
           dist = 50, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)

ww2_death_no<-reposition_legend(ww2_death_no, 'bottom left')
ggsave(ww2_death_no,file="./Dropbox/Legacies_Project/Paper/figures/Figure_A12c.jpg", 
       height=19.42, width=23.2, units = "cm", dpi=300)


#################
#MAP 4 Massacres#
#################

#Points
concentration_camps_proj$prisoners
massacres_lbl<-subset(concentration_camps_proj, select = c("camp_name","lon", "lat"))

#Let's duplicate the data table to avoid accidental overwriting. Define latitude and longitude:
value_no <- subset(concentration_camps_proj, !is.na(lon) & !is.na(lat) & !is.na(prisoners))
value_no$x <- value_no$lon  # define x & y as longitude and latitude
value_no$y <- value_no$lat

value<-subset(concentration_camps_proj, select = c(deaths , lon, lat))
names(value) <- c("death_no", "x", "y", "Shape")
value_no <- subset(value, !is.na(x) & !is.na(y) & !is.na(death_no))

#Set spatial coordinates to create a Spatial object:
extent(value_no)

#Plot the results:
plot(value_no)
extent(HRV_adm0)


##### IDW interpolation #####
# establish an extent within which you want to interpolate
# -99/24 to -80/32.
x_range <- as.numeric(c(extent(HRV_adm0)[1], extent(HRV_adm0)[2]))  # min/max longitude of the interpolation area
y_range <- as.numeric(c(extent(HRV_adm0)[3], extent(HRV_adm0)[4]))  # min/max latitude of the interpolation area

# create an empty grid of values ranging from the xmin-xmax, ymin-ymax
grd <- expand.grid(x = seq(from = x_range[1],
                           to = x_range[2], 
                           by = 0.02),
                   y = seq(from = y_range[1],
                           to = y_range[2], 
                           by = 0.02))  # expand points to grid

coordinates(grd) <- ~x + y
# turn into a spatial pixels object
gridded(grd) <- TRUE
#turn into sf object
grd<-as(grd, "sf")



#### view grid with points overlayed
plot(grd, cex = 1.5, col = "grey")
plot(value_no,
     pch = 15,
     col = "red",
     cex = 1, add=T)
plot(HRV_adm0,add=T)


value_no<-value_no[!sf::st_is_empty(value_no), ]
value_no<-na.omit(value_no)
grd <- sf::st_set_crs(grd, sf::st_crs(value_no))


#IDW interpolation
g = gstat(formula = log(death_no+1) ~ 1, data = value_no)
z = predict(g, grd)
z2<-subset(z, select = var1.pred)
names(z2)[names(z2)=="var1.pred"]<-"return"


z3 <- z2 %>%
  mutate(lon = unlist(map(z2$geometry,1)),
         lat = unlist(map(z2$geometry,2)))

idw_crp<-st_intersection(z3, HRV_adm0)

ww2_concamp<-ggplot()+
  geom_tile(data = idw_crp, 
            alpha = 0.9,
            aes(x = lon,
                y = lat,
                fill = round(return, 0)))+
  scale_fill_gradient(low = "white", high = "black", guide = guide_legend(title = "Log No. of Deaths"))+
  geom_sf(data = mil_col_short_polygon, colour = "blue", fill = NA, linewidth = 1.5)+
  geom_sf(data = HRV_adm0, colour = "black", fill = NA, linewidth = 0.8)+
  geom_point(aes(x = concentration_camps_proj$lon, y = concentration_camps_proj$lat, 
                 size=concentration_camps_proj$prisoners), colour = "red", shape=16, alpha=0.8)+
  scale_size(name   = "No. of Prisoners")+
  geom_text_repel(data = concentration_camps_proj, aes(lon, lat, label = camp_name))+
  labs(colour="Concentration Camps")+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  #ggtitle("Concentration Camps in Croatia during WW2")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
           #Above you mention how long the scalebar should be
           y.min = 46.4, y.max = 46.7,
           #Above you mention how thick the scalebar should be
           dist = 50, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)

ww2_concamp<-reposition_legend(ww2_concamp, 'bottom left')

ggsave(ww2_concamp, file="./Dropbox/Legacies_Project/Paper/figures/Figure_A12b.jpg", 
       height=19.42, width=24.2, units = "cm", dpi=300)

###########################
#MAP 5 Concentration Camps#
###########################
#Points

thor_ww2_croatia_proj_lbl<-subset(thor_ww2_croatia_proj, select = c("COUNTRY_FLYING_MISSION","LONGITUDE", "LATITUDE"))
value<-subset(thor_ww2_croatia_proj, select = c("TONS_OF_HE","LONGITUDE", "LATITUDE"))
names(value) <- c("Tons_of_TNT", "lon", "lat", "Shape")
value_no <- subset(value, !is.na(lon) & !is.na(lat) & !is.na(Tons_of_TNT))

#Let's duplicate the data table to avoid accidental overwriting. Define latitude and longitude:
value_no$x<-value_no$lon
value_no$y<-value_no$lat

#Set spatial coordinates to create a Spatial object:
extent(value_no)

##### IDW interpolation #####
# establish an extent within which you want to interpolate
# -99/24 to -80/32.
x_range <- as.numeric(c(extent(HRV_adm0)[1], extent(HRV_adm0)[2]))  # min/max longitude of the interpolation area
y_range <- as.numeric(c(extent(HRV_adm0)[3], extent(HRV_adm0)[4]))  # min/max latitude of the interpolation area

# create an empty grid of values ranging from the xmin-xmax, ymin-ymax
grd <- expand.grid(x = seq(from = x_range[1],
                           to = x_range[2], 
                           by = 0.02),
                   y = seq(from = y_range[1],
                           to = y_range[2], 
                           by = 0.02))  # expand points to grid


coordinates(grd) <- ~x + y
# turn into a spatial pixels object
gridded(grd) <- TRUE
#turn into sf object
grd<-as(grd, "sf")

#### view grid with points overlayed
plot(grd, cex = 1.5, col = "grey")
plot(value_no,
     pch = 15,
     col = "red",
     cex = 1, add=T)
plot(HRV_adm0,add=T)


value_no<-value_no[!sf::st_is_empty(value_no), ]
value_no<-na.omit(value_no)
grd <- sf::st_set_crs(grd, sf::st_crs(value_no))


#IDW interpolation
g = gstat(formula = log(Tons_of_TNT+1) ~ 1, data = value_no)
z = predict(g, grd)
z2<-subset(z, select = var1.pred)
names(z2)[names(z2)=="var1.pred"]<-"return"


z3 <- z2 %>%
  mutate(lon = unlist(map(z2$geometry,1)),
         lat = unlist(map(z2$geometry,2)))

idw_crp<-st_intersection(z3, HRV_adm0)
plot(idw_crp)


ww2_bombs<-ggplot()+
  geom_tile(data = idw_crp, 
            alpha = 0.9,
            aes(x = lon,
                y = lat,
                fill = round(return, 0)))+
  scale_fill_gradient(low = "white", high = "black", 
                      guide = guide_legend(title = "Log Tons of TNT"))+
  geom_sf(data = mil_col_short_polygon, colour = "blue", fill = NA, linewidth = 1.5)+
  geom_sf(data = HRV_adm0, colour = "black", fill = NA, linewidth = 0.8)+
  geom_point(aes(x = thor_ww2_croatia_proj_lbl$lon, y = thor_ww2_croatia_proj_lbl$lat, 
                 colour = thor_ww2_croatia_proj_lbl$Perpetrator), size=3)+
  labs(colour="Perpetrator")+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        #Legend.position values should be between 0 and 1. c(0,0) corresponds to the "bottom left"
        #and c(1,1) corresponds to the "top right" position.
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 18.5, x.max = 19.1,
           #Above you mention how long the scalebar should be
           y.min = 46.4, y.max = 46.7,
           #Above you mention how thick the scalebar should be
           dist = 50, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)

ww2_bombs<-reposition_legend(ww2_bombs, 'bottom left')


ggsave(ww2_bombs, file="./Dropbox/Legacies_Project/Paper/figures/Figure_A12a.jpg", 
       height=19.42, width=24.2, units = "cm", dpi=300)


#####################################################
#MAP 5 Overlap between Modern and Historical Krajina#
#####################################################

cols.fill = c("Modern \nKrajina" = alpha("blue", 0.5), 
              "Historical \nKrajina" = alpha("red", 0.5))


map<-ggplot()+
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude in m", guide="none")  + 
  geom_sf(data = all_coun, fill=NA, color = "black", size = 0.4)+
  geom_sf(data = mil_col_short_polygon, aes(fill = "Historical \nKrajina"), color = "grey60", size = 0.4)+
  geom_sf(data = occupied, aes(fill = "Modern \nKrajina"), color = "grey60", size = 0.4)+
  geom_point(aes(x = ucdp_events_croatia$longitude, y = ucdp_events_croatia$latitude, 
                 size=ucdp_events_croatia$best),
             colour = "red", shape=16, alpha=0.8)+
  scale_fill_manual(name = "Territory", values = cols.fill) +
  scale_size(name   = "No. of Deaths")+
  coord_sf(ylim=c(42.4,46.669853), xlim=c(13.556495, 19.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 38, x.max = 40,
                 #Above you mention how long the scalebar should be
                 y.min = 50.5, y.max = 52,
                 #Above you mention how thick the scalebar should be
                 dist = 250, dist_unit = "km",
                 transform = T, model = "WGS84",
                 location = "topright",
                 st.size = 4,
                 #Above you have the font size of the numbers below the scalebar
                 st.dist =0.2,
                 #Above you have the distance between the bar and the text, as a proportion of the y axis.
                 height=0.2)
# #Above you have anumber between 0 and 1 to indicate the height 
# #of the scale bar, as a proportion of the y axis

map<-reposition_legend(map, 'bottom left')

ggsave(map,file="./Dropbox/Legacies_Project/Paper/figures/Figure_A13a.jpg", 
       height=19.42, width=20.2, units = "cm", dpi=300)


##########
#FOR BOOK#
##########

ggsave(map,file="./Dropbox/Book Project/Book_manuscript_RR/chapter_04_appendix_tables/figure_A1.jpg", 
       height=19.42, width=20.2, units = "cm", dpi=300)

